<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;

class UtilsController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionUnixtime()
    {
        return $this->render('unixtime');
    }

    /**
     * @return mixed
     */
    public function actionDo()
    {
        $input = Yii::$app->request->post('input');
        $data = $this->dealParams($input);

        if (!isset($data['item']) || !in_array($data['item'], ['unixToDate', 'dateToUnix', 'timeStamp', 'untoch', 'chtoun', 'conv', 'res'])) {
            return Yii::$app->tool->respJsonData('-1', '参数错误');
        }

        switch ($data['item']) {
            case 'timeStamp'://刷新时间戳
                $return = time();
                break;

            case 'unixToDate'://时间戳转日期
                if (empty($data['unixTime'])) {
                    return Yii::$app->tool->respJsonData('-1', '时间戳不能为空');
                }

                $timestamp = intval($data['unixTime']);
                if (!is_int($timestamp)) {
                    return Yii::$app->tool->respJsonData('-1', '时间戳格式有误');
                }

                if ($data['type'] == 1) {
                    $timestamp = floor($timestamp / 1000);
                }

                $return = date('Y-m-d H:i:s', $timestamp);
                break;
            case 'dateToUnix'://日期转时间戳
                if (empty($data['date'])) {
                    return Yii::$app->tool->respJsonData('-1', '日期不能为空');
                }

                $return = strtotime($data['date']);
                break;

            case 'untoch':
                if (empty($data['textarea'])) {
                    return Yii::$app->tool->respJsonData('-1', '请把你需要转换的内容粘贴在广本域中');
                }

                $return = json_encode($data['textarea']);
                break;

            case 'chtoun':
                if (empty($data['textarea'])) {
                    return Yii::$app->tool->respJsonData('-1', '请把你需要转换的内容粘贴在广本域中');
                }
                $json = '{"str":"' . $data['textarea'] . '"}';
                $decode = json_decode($json, true);
                $return = $decode['str'];

                break;

            case 'conv':
                if (empty($data['textarea'])) {
                    return Yii::$app->tool->respJsonData('-1', '请把你需要转换的内容粘贴在广本域中');
                }

                $return = utf8_encode($data['textarea']);
                break;

            case 'res':
                if (empty($data['textarea'])) {
                    return Yii::$app->tool->respJsonData('-1', '请把你需要转换的内容粘贴在广本域中');
                }

                $return = utf8_decode($data['textarea']);
                break;
        }

        return Yii::$app->tool->respJsonData(0, 'success', $return);
    }

    /**
     * @return string
     */
    public function actionUnicode()
    {
        return $this->render('unicode');
    }

    /**
     * @return string
     */
    public function actionUtf8()
    {
        return $this->render('utf8');
    }

    /**
     * @return string
     */
    public function actionUrlencode()
    {
        return $this->render('urlencode');
    }

    /**
     * [dealParams description]
     * @param  [type] $input [description]
     * @return [type]        [description]
     */
    private function dealParams($input)
    {
        $return = [];

        $input = trim($input, '&');
        $data = explode('&', $input);
        foreach ($data as $key => $value) {
            $vs = explode('=', $value);
            $return[$vs[0]] = isset($vs[1]) ? $vs[1] : '';
        }

        return $return;
    }

    public function flight_kill_zero()
    {
        $str = Yii::$app->request->get('str', '');
        $num = Yii::$app->request->get('num', 2);

        if(is_numeric($str)){
            echo $str;die;
        }

        $head=substr($str, 0, $num);
        $str=substr($str, $num);
        $str = str_split($str);
        foreach($str as $key=>$value){
            if(is_numeric($value) && !empty($value)){
                    break;
            }else{
                if(is_numeric($value) && empty($value)){
                    unset($str[$key]);
                }
            }
        }
        echo $head.implode('',$str);die;
    }
}
